1include <../std.scad>
  2
  3
  4// Section: List Query Operations
  5
  6module test_is_homogeneous(){
  7    assert(is_homogeneous([[1,["a"]], [2,["b"]]])==true);
  8    assert(is_homogeneous([[1,["a"]], [2,[true]]])==false);
  9    assert(is_homogeneous([[1,["a"]], [2,[true]]],1)==true);
 10    assert(is_homogeneous([[1,["a"]], [2,[true]]],2)==false);
 11    assert(is_homogeneous([[1,["a"]], [true,["b"]]])==false);
 12}
 13test_is_homogeneous();
 14
 15
 16module test_select() {
 17    l = [3,4,5,6,7,8,9];
 18    assert(select(l, 5, 6) == [8,9]);
 19    assert(select(l, 5, 8) == [8,9,3,4]);
 20    assert(select(l, 5, 2) == [8,9,3,4,5]);
 21    assert(select(l, -3, -1) == [7,8,9]);
 22    assert(select(l, 3, 3) == [6]);
 23    assert(select(l, 4) == 7);
 24    assert(select(l, -2) == 8);
 25    assert(select(l, [1:3]) == [4,5,6]);
 26    assert(select(l, [1,3]) == [4,6]);
 27}
 28test_select();
 29
 30
 31module test_slice() {
 32    l = [3,4,5,6,7,8,9];
 33    assert(slice(l, 5, 6) == [8,9]);
 34    assert(slice(l, 5, 8) == [8,9]);
 35    assert(slice(l, 5, 2) == []);
 36    assert(slice(l, -3, -1) == [7,8,9]);
 37    assert(slice(l, 3, 3) == [6]);
 38    assert(slice(l, 4) == [7,8,9]);
 39    assert(slice(l, -2) == [8,9]);
 40    assert(slice(l,-10,-8) == []);
 41    assert(slice(l,10,12) == []);
 42    assert(slice(l,12,10) == []);
 43    assert(slice(l,4,12) == [7,8,9]);
 44    assert(slice(l,-10,2) == [3,4,5]);
 45    assert(slice(l,-10,-4) == [3,4,5,6]);
 46    assert(slice(l,-1,1) == []);
 47    assert(slice(l,5,4) == []);
 48}
 49test_slice();
 50
 51
 52module test_last() {
 53    list = [1,2,3,4];
 54    assert(last(list)==4);
 55    assert(last([])==undef);
 56}
 57test_last();
 58
 59
 60module test_list_head() {
 61    list = [1,2,3,4];
 62    assert_equal(list_head(list), [1,2,3]);
 63    assert_equal(list_head([1]), []);
 64    assert_equal(list_head([]), []);
 65    assert_equal(list_head(list,-3), [1,2]);
 66    assert_equal(list_head(list,1), [1,2]);
 67    assert_equal(list_head(list,2), [1,2,3]);
 68    assert_equal(list_head(list,6), [1,2,3,4]);
 69    assert_equal(list_head(list,-6), []);
 70}
 71test_list_head();
 72
 73
 74module test_list_tail() {
 75    list = [1,2,3,4];
 76    assert_equal(list_tail(list), [2,3,4]);
 77    assert_equal(list_tail([1]), []);
 78    assert_equal(list_tail([]), []);
 79    assert_equal(list_tail(list,-3), [2,3,4]);
 80    assert_equal(list_tail(list,2), [3,4]);
 81    assert_equal(list_tail(list,3), [4]);
 82    assert_equal(list_tail(list,6), []);
 83    assert_equal(list_tail(list,-6), [1,2,3,4]);
 84}
 85test_list_tail();
 86
 87
 88module test_in_list() {
 89    assert(in_list("bar", ["foo", "bar", "baz"]));
 90    assert(!in_list("bee", ["foo", "bar", "baz"]));
 91    assert(in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1));
 92    assert(!in_list("bee", ["foo", "bar", ["bee"]]));
 93    assert(in_list(NAN, [NAN])==false);
 94    assert(!in_list(undef, [3,4,5]));
 95    assert(in_list(undef,[3,4,undef,5]));
 96    assert(!in_list(3,[]));
 97    assert(!in_list(3,[4,5,[3]]));
 98}
 99test_in_list();
100
101
102
103
104// Section: Basic List Generation
105
106module test_repeat() {
107    assert(repeat(1, 4) == [1,1,1,1]);
108    assert(repeat(8, [2,3]) == [[8,8,8], [8,8,8]]);
109    assert(repeat(0, [2,2,3]) == [[[0,0,0],[0,0,0]], [[0,0,0],[0,0,0]]]);
110    assert(repeat([1,2,3],3) == [[1,2,3], [1,2,3], [1,2,3]]);
111    assert(repeat(4, [2,-1]) == [[], []]);
112}
113test_repeat();
114
115
116module test_count() {
117    assert_equal(count(5), [0,1,2,3,4]);
118    assert_equal(count(5,3), [3,4,5,6,7]);
119    assert_equal(count(4,3,2), [3,5,7,9]);
120    assert_equal(count(5,0,0.25), [0, 0.25, 0.5, 0.75, 1.0]);
121}
122test_count();
123
124
125module test_reverse() {
126    assert(reverse([3,4,5,6]) == [6,5,4,3]);
127    assert(reverse("abcd") == "dcba");
128    assert(reverse([]) == []);
129}
130test_reverse();
131
132
133module test_list_rotate() {
134    assert(list_rotate([1,2,3,4,5],-2) == [4,5,1,2,3]);
135    assert(list_rotate([1,2,3,4,5],-1) == [5,1,2,3,4]);
136    assert(list_rotate([1,2,3,4,5],0) == [1,2,3,4,5]);
137    assert(list_rotate([1,2,3,4,5],1) == [2,3,4,5,1]);
138    assert(list_rotate([1,2,3,4,5],2) == [3,4,5,1,2]);
139    assert(list_rotate([1,2,3,4,5],3) == [4,5,1,2,3]);
140    assert(list_rotate([1,2,3,4,5],4) == [5,1,2,3,4]);
141    assert(list_rotate([1,2,3,4,5],5) == [1,2,3,4,5]);
142    assert(list_rotate([1,2,3,4,5],6) == [2,3,4,5,1]);
143    assert(list_rotate([],3) == []);
144    path = [[1,1],[-1,1],[-1,-1],[1,-1]];
145    assert(list_rotate(path,1) == [[-1,1],[-1,-1],[1,-1],[1,1]]);
146    assert(list_rotate(path,2) == [[-1,-1],[1,-1],[1,1],[-1,1]]);
147}
148test_list_rotate();
149
150
151
152module test_list_set() {
153    assert_equal(list_set([2,3,4,5], 2, 21), [2,3,21,5]);
154    assert_equal(list_set([2,3,4,5], [1,3], [81,47]), [2,81,4,47]);
155    assert_equal(list_set([2,3,4,5], [2], [21]), [2,3,21,5]);
156    assert_equal(list_set([1,2,3], [], []), [1,2,3]);
157    assert_equal(list_set([1,2,3], [1,5], [4,4]), [1,4,3,0,0,4]);
158    assert_equal(list_set([1,2,3], [1,5], [4,4],dflt=12), [1,4,3,12,12,4]);
159    assert_equal(list_set([1,2,3], [1,2], [4,4],dflt=12, minlen=5), [1,4,4,12,12]);
160    assert_equal(list_set([1,2,3], 1, 4, dflt=12, minlen=5), [1,4,3,12,12]);
161    assert_equal(list_set([1,2,3], [],[],dflt=12, minlen=5), [1,2,3,12,12]);
162    assert_equal(list_set([1,2,3], 5,9), [1,2,3,0,0,9]);
163    assert_equal(list_set([1,2,3], 5,9,minlen=4), [1,2,3,0,0,9]);
164    assert_equal(list_set([1,2,3], 5,9,minlen=7), [1,2,3,0,0,9,0]);
165    assert_equal(list_set([1,2,3], 5,9,dflt=12), [1,2,3,12,12,9]);
166    assert_equal(list_set([1,2,3], -1,12), [1,2,12]);
167    assert_equal(list_set([1,2,3], -1,12,minlen=5), [1,2,12,0,0]);
168    assert_equal(list_set([1,2,3], [-2,5], [8,9]), [1,8,3,0,0,9]);
169    assert_equal(list_set([1,2,3], [-2,5], [8,9],minlen=8,dflt=-1), [1,8,3,-1,-1,9,-1,-1]);
170    assert_equal(list_set([1,2,3], [-2,5], [8,9],minlen=3,dflt=-1), [1,8,3,-1,-1,9]);
171    assert_equal(list_set([1,2,3], [0],[4], minlen=5), [4,2,3,0,0]);
172    assert_equal(list_set([], 2,3), [0,0,3]);
173    assert_equal(list_set([], 2,3,minlen=5,dflt=1), [1,1,3,1,1]);
174}
175test_list_set();
176
177
178module test_list_remove() {
179    assert(list_remove([3,6,9,12],1) == [3,9,12]);
180    assert(list_remove([3,6,9,12],[1]) == [3,9,12]);
181    assert(list_remove([3,6,9,12],[1,3]) == [3,9]);
182    assert(list_remove([3,6,9],[]) == [3,6,9]);
183    assert(list_remove([],[]) == []);
184    assert(list_remove([1,2,3], -1)==[1,2,3]);
185    assert(list_remove([1,2,3], 3)==[1,2,3]);    
186    assert(list_remove([1,2,3], [-1,3])==[1,2,3]);    
187    assert(list_remove([1,2,3], [-1,1,3])==[1,3]);    
188}
189test_list_remove();
190
191module test_list_remove_values() {
192    animals = ["bat", "cat", "rat", "dog", "bat", "rat"];
193    assert(list_remove_values(animals, "rat") == ["bat","cat","dog","bat","rat"]);
194    assert(list_remove_values(animals, "bat", all=true) == ["cat","rat","dog","rat"]);
195    assert(list_remove_values(animals, ["bat","rat"]) == ["cat","dog","bat","rat"]);
196    assert(list_remove_values(animals, ["bat","rat"], all=true) == ["cat","dog"]);
197    assert(list_remove_values(animals, ["tucan","rat"], all=true) == ["bat","cat","dog","bat"]);
198
199    test = [3,4,[5,6],7,5,[5,6],4,[6,5],7,[4,4]];
200    assert_equal(list_remove_values(test,4), [3, [5, 6], 7, 5, [5, 6], 4, [6, 5], 7, [4, 4]]);
201    assert_equal(list_remove_values(test,[4,4]), [3, [5, 6], 7, 5, [5, 6], [6, 5], 7, [4, 4]]);
202    assert_equal(list_remove_values(test,[4,7]), [3, [5, 6], 5, [5, 6], 4, [6, 5], 7, [4, 4]]);
203    assert_equal(list_remove_values(test,[5,6]), [3, 4, [5, 6], 7, [5, 6], 4, [6, 5], 7, [4, 4]]);
204    assert_equal(list_remove_values(test,[[5,6]]), [3,4,7,5,[5,6],4,[6,5],7,[4,4]]);
205    assert_equal(list_remove_values(test,[[5,6]],all=true), [3,4,7,5,4,[6,5],7,[4,4]]);    
206    assert_equal(list_remove_values(test,4,all=true),  [3, [5, 6], 7, 5, [5, 6], [6, 5],7, [4, 4]]);
207    assert_equal(list_remove_values(test,[4,7],all=true), [3, [5, 6], 5, [5, 6], [6, 5], [4, 4]]);
208    assert_equal(list_remove_values(test,[]),test);
209    assert_equal(list_remove_values(test,[],all=true),test);
210    assert_equal(list_remove_values(test,99), test);
211    assert_equal(list_remove_values(test,99,all=true), test);
212    assert_equal(list_remove_values(test,[99,100],all=true), test);
213    assert_equal(list_remove_values(test,[99,100]), test);            
214}
215test_list_remove_values();
216
217
218module test_list_insert() {
219    assert_equal(list_insert([3,6,9,12],1,5),[3,5,6,9,12]);
220    assert_equal(list_insert([3,6,9,12],[1,3],[5,11]),[3,5,6,9,11,12]);
221    assert_equal(list_insert([3],1,4), [3,4]);
222    assert_equal(list_insert([3],[0,1], [1,2]), [1,3,2]);
223    assert_equal(list_insert([1,2,3],[],[]),[1,2,3]);
224    assert_equal(list_insert([], 0, 4),[4]);
225    assert_equal(list_insert([1,2,3],-2,4), [1,4,2,3]);
226    assert_equal(list_insert([1,2,3,4,5], [-1,-3],[12,9]), [1,2,9,3,4,12,5]);
227}
228test_list_insert();
229
230
231module test_bselect() {
232    assert(bselect([3,4,5,6,7], [false,false,false,false,false]) == []);
233    assert(bselect([3,4,5,6,7], [false,true,true,false,true]) == [4,5,7]);
234    assert(bselect([3,4,5,6,7], [true,true,true,true,true]) == [3,4,5,6,7]);
235}
236test_bselect();
237
238
239module test_list_bset() {
240    assert(list_bset([false,true,false,true,false], [3,4]) == [0,3,0,4,0]);
241    assert(list_bset([false,true,false,true,false], [3,4], dflt=1) == [1,3,1,4,1]);
242}
243test_list_bset();
244
245
246module test_min_length() {
247    assert(min_length(["foobar", "bazquxx", "abcd"]) == 4);
248}
249test_min_length();
250
251
252module test_max_length() {
253    assert(max_length(["foobar", "bazquxx", "abcd"]) == 7);
254}
255test_max_length();
256
257
258module test_list_pad() {
259    assert(list_pad([4,5,6], 5, 8) == [4,5,6,8,8]);
260    assert(list_pad([4,5,6,7,8], 5, 8) == [4,5,6,7,8]);
261    assert(list_pad([4,5,6,7,8,9], 5, 8) == [4,5,6,7,8,9]);
262}
263test_list_pad();
264
265
266module test_idx() {
267    colors = ["red", "green", "blue", "cyan"];
268    assert([for (i=idx(colors)) i] == [0,1,2,3]);
269    assert([for (i=idx(colors,e=-2)) i] == [0,1,2]);
270    assert([for (i=idx(colors,s=1)) i] == [1,2,3]);
271    assert([for (i=idx(colors,s=1,e=-2)) i] == [1,2]);
272}
273test_idx();
274
275
276module test_shuffle() {
277    nums1 = count(100);
278    nums2 = shuffle(nums1,33);
279    nums3 = shuffle(nums2,99);
280    assert(sort(nums2)==nums1);
281    assert(sort(nums3)==nums1);
282    assert(nums1!=nums2);
283    assert(nums2!=nums3);
284    assert(nums1!=nums3);
285    str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
286    shufstr = shuffle(str,12);
287    assert(shufstr != str && sort(shufstr)==str);
288}
289test_shuffle();
290
291
292
293// Sets
294
295module test_set_union() {
296    assert_equal(
297        set_union([2,3,5,7,11], [1,2,3,5,8]),
298        [2,3,5,7,11,1,8]
299    );
300    assert_equal(
301        set_union([2,3,5,7,11], [1,2,3,5,8], get_indices=true),
302        [[5,0,1,2,6],[2,3,5,7,11,1,8]]
303    );
304}
305test_set_union();
306
307
308module test_set_difference() {
309    assert_equal(
310        set_difference([2,3,5,7,11], [1,2,3,5,8]),
311        [7,11]
312    );
313}
314test_set_difference();
315
316
317module test_set_intersection() {
318    assert_equal(
319        set_intersection([2,3,5,7,11], [1,2,3,5,8]),
320        [2,3,5]
321    );
322}
323test_set_intersection();
324
325
326// Arrays
327
328
329module test_force_list() {
330    assert_equal(force_list([3,4,5]), [3,4,5]);
331    assert_equal(force_list(5), [5]);
332    assert_equal(force_list(7, n=3), [7,7,7]);
333    assert_equal(force_list(4, n=3, fill=1), [4,1,1]);
334}
335test_force_list();
336
337
338module test_pair() {
339    assert(pair([3,4,5,6]) == [[3,4], [4,5], [5,6]]);
340    assert(pair("ABCD") == [["A","B"], ["B","C"], ["C","D"]]);
341    assert(pair([3,4,5,6],true) == [[3,4], [4,5], [5,6], [6,3]]);
342    assert(pair("ABCD",true) == [["A","B"], ["B","C"], ["C","D"], ["D","A"]]);
343    assert(pair([3,4,5,6],wrap=true) == [[3,4], [4,5], [5,6], [6,3]]);
344    assert(pair("ABCD",wrap=true) == [["A","B"], ["B","C"], ["C","D"], ["D","A"]]);
345    assert_equal(pair([],wrap=true),[]);
346    assert_equal(pair([],wrap=false),[]);
347    assert_equal(pair([1],wrap=true),[]);
348    assert_equal(pair([1],wrap=false),[]);
349    assert_equal(pair([1,2],wrap=false),[[1,2]]);
350    assert_equal(pair([1,2],wrap=true),[[1,2],[2,1]]);
351}
352test_pair();
353
354
355module test_triplet() {
356    assert(triplet([3,4,5,6,7]) == [[3,4,5], [4,5,6], [5,6,7]]);
357    assert(triplet("ABCDE") == [["A","B","C"], ["B","C","D"], ["C","D","E"]]);
358    assert(triplet([3,4,5,6],true) == [[6,3,4],[3,4,5], [4,5,6], [5,6,3]]);
359    assert(triplet("ABCD",true) == [["D","A","B"],["A","B","C"], ["B","C","D"], ["C","D","A"]]);
360    assert(triplet("ABCD",wrap=true) == [["D","A","B"],["A","B","C"], ["B","C","D"], ["C","D","A"]]);
361    assert_equal(triplet([],wrap=true),[]);
362    assert_equal(triplet([],wrap=false),[]);    
363    assert_equal(triplet([1],wrap=true),[]);
364    assert_equal(triplet([1],wrap=false),[]);    
365    assert_equal(triplet([1,2],wrap=true),[]);
366    assert_equal(triplet([1,2],wrap=false),[]);    
367    assert_equal(triplet([1,2,3],wrap=true),[[3,1,2],[1,2,3],[2,3,1]]);
368    assert_equal(triplet([1,2,3],wrap=false),[[1,2,3]]);    
369}
370test_triplet();
371
372
373module test_combinations() {
374    assert(combinations([3,4,5,6]) ==  [[3,4],[3,5],[3,6],[4,5],[4,6],[5,6]]);
375    assert(combinations([3,4,5,6],n=3) == [[3,4,5],[3,4,6],[3,5,6],[4,5,6]]);
376}
377test_combinations();
378
379
380module test_repeat_entries() {
381    list = [0,1,2,3];
382    assert(repeat_entries(list, 6) == [0,0,1,2,2,3]);
383    assert(repeat_entries(list, 6, exact=false) == [0,0,1,1,2,2,3,3]);
384    assert(repeat_entries(list, [1,1,2,1], exact=false) == [0,1,2,2,3]);
385}
386test_repeat_entries();
387
388
389module test_list_to_matrix() {
390    v = [1,2,3,4,5,6];
391    assert(list_to_matrix(v,2) == [[1,2], [3,4], [5,6]]);
392    assert(list_to_matrix(v,3) == [[1,2,3], [4,5,6]]);
393    assert(list_to_matrix(v,4,0) == [[1,2,3,4], [5,6,0,0]]);
394}
395test_list_to_matrix();
396
397
398module test_flatten() {
399    assert(flatten([[1,2,3], [4,5,[6,7,8]]]) == [1,2,3,4,5,[6,7,8]]);
400    assert(flatten([]) == []);
401}
402test_flatten();
403
404
405module test_full_flatten() {
406    assert(full_flatten([[1,2,3], [4,5,[6,[7],8]]]) == [1,2,3,4,5,6,7,8]);
407    assert(full_flatten([]) == []);
408}
409test_full_flatten();
410
411
412module test_list_shape() {
413    assert(list_shape([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]) == [2,2,3]);
414    assert(list_shape([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], 0) == 2);
415    assert(list_shape([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], 2) == 3);
416    assert(list_shape([[[1,2,3],[4,5,6]],[[7,8,9]]]) == [2,undef,3]);
417    assert(list_shape([1,2,3,4,5,6,7,8,9]) == [9]);
418    assert(list_shape([[1],[2],[3],[4],[5],[6],[7],[8],[9]]) == [9,1]);
419    assert(list_shape([]) == [0]);
420    assert(list_shape([[]]) == [1,0]);
421    assert(list_shape([[],[]]) == [2,0]);
422    assert(list_shape([[],[1]]) == [2,undef]);
423}
424test_list_shape();
425
426
427// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap